package com.warm.micro.common.vo;

import com.warm.micro.common.exception.CommonException;
import lombok.Data;

/**
 * @Date: 2020/5/8 14:19
 * @Author: LChuang
 * @Description:
 */
@Data
public class Resp<T> {

    private Integer code;

    private String msg;

    private T data;

    private Resp(){}

    /**
     * 成功但是无参数
     * @return
     */
    public static Resp success(){
        Resp response = new Resp();
        response.setCode(200);
        response.setMsg("success");
        return response;
    }

    /**
     * 成功有参数
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Resp success(T data){
        Resp response = new Resp();
        response.setCode(200);
        response.setMsg("success");
        response.setData(data);
        return response;
    }

    /**
     * 成功但是无参数
     * @return
     */
    public static Resp error(){
        Resp response = new Resp();
        response.setCode(501);
        response.setMsg("error");
        return response;
    }

    /**
     * 成功有参数
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Resp error(T data){
        Resp response = new Resp();
        response.setCode(501);
        response.setMsg("error");
        response.setData(data);
        return response;
    }


    /**
     * 未登录异常
     * @param <M>
     * @return
     */
    public static<M> Resp noLogin(){
        Resp response = new Resp();
        response.setCode(401);
        response.setMsg("请登录");
        return response;
    }

    /**
     * 出现业务异常
     * @param e
     * @param <T>
     * @return
     */
    public static<T> Resp commonException(CommonException e){
        Resp response = new Resp();
        response.setCode(e.getCode());
        response.setMsg(e.getMsg());
        return response;
    }

    public static<T> Resp nullException(NullPointerException e){
        Resp response = new Resp();
        response.setMsg(e.getMessage());
        return response;
    }
}
